package cn.junki.querydsljpademo.config;

import com.querydsl.jpa.impl.JPAQueryFactory;
import com.querydsl.sql.MySQLTemplates;
import com.querydsl.sql.SQLQueryFactory;
import com.querydsl.sql.SQLTemplates;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.persistence.EntityManager;
import javax.sql.DataSource;

/**
 * JPA+QueryDSL查询工厂
 *
 * @author Junki
 * @date 2023-09-05 10:30:21
 */
@Configuration
public class QueryFactoryConfig {
 
    @Bean
    public JPAQueryFactory jpaQueryFactory(EntityManager entityManager) {
        return new JPAQueryFactory(entityManager);
    }

    @Bean
    public SQLQueryFactory sqlQueryFactory(DataSource dataSource){
        SQLTemplates sqlTemplates = MySQLTemplates.builder().build();
        return new SQLQueryFactory(new com.querydsl.sql.Configuration(sqlTemplates), dataSource);
    }

}